<?php 
class ControllerProductAttributeGroup extends Controller { 
	private $error = array();
   
  	public function index() {
		$this->load->language('product/attribute_group');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/attribute_group');
    	$this->getList();
  	}
              
  	public function insert() {
		$this->load->language('product/attribute_group');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/attribute_group');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
      		$this->model_product_attribute_group->addAttributeGroup($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
      		$this->redirect($this->url->link('product/attribute_group', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getFormText();
    	$this->getInsertForm();
  	}

  	public function update() {
		$this->load->language('product/attribute_group');
    	$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('product/attribute_group');
    	if ($this->request->server['REQUEST_METHOD'] == 'POST') {
	  		$this->model_product_attribute_group->editAttributeGroup($this->request->get['attribute_group_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('product/attribute_group', 'token=' . $this->session->data['token'], 'SSL'));
    	}
    	$this->getFormText();
    	$this->getUpdateForm();
  	}
    
  	private function getList() {
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/attribute_group', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('product/attribute_group/insert', 'token=' . $this->session->data['token'], 'SSL');
		
  		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_action'] = $this->language->get('column_action');		
		
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_filter'] = $this->language->get('button_filter');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
		$url = '';
  		if (isset($this->request->get['filter_display_name'])) {
			$filter_display_name = $this->request->get['filter_display_name'];
			$url .= '&filter_display_name=' . $this->request->get['filter_display_name'];
		} else {
			$filter_display_name = "";
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$this->data['attribute_groups'] = array();
		$data = array(
			'filter_display_name'  => $filter_display_name,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		$attribute_group_total = $this->model_product_attribute_group->getTotalAttributeGroups($data);
		$results = $this->model_product_attribute_group->getAttributeGroups($data);
    	foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('product/attribute_group/update', 'token=' . $this->session->data['token'] . '&attribute_group_id=' . $result['attribute_group_id'], 'SSL')
			);
			$action[] = array(
				'text' => $this->language->get('text_attribute_from_group'),
				'href' => $this->url->link('product/attribute', 'token=' . $this->session->data['token'] . '&filter_group_id=' . $result['attribute_group_id'], 'SSL')
			);
			$this->data['attribute_groups'][] = array(
				'attribute_group_id' => $result['attribute_group_id'],
				'name'               => $result['name'],
				'display_name'       => $result['display_name'],
				'action'             => $action
			);
		}
		
		$this->data['token'] = $this->session->data['token'];
		$this->data['filter_display_name'] = $filter_display_name;

		$pagination = new Pagination();
		$pagination->total = $attribute_group_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('product/attribute_group', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();

		$this->template = 'product/attribute_group_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());
  	}
  
  	private function getFormText() {
     	$this->data['heading_title'] = $this->language->get('heading_title');
    	$this->data['entry_name'] = $this->language->get('entry_name');
    	$this->data['entry_display_name'] = $this->language->get('entry_display_name');

    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
    
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = array();
		}

  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),    		
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('product/attribute_group', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
   		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
  	}
  	
	private function getInsertForm() {
     	$this->data['action'] = $this->url->link('product/attribute_group/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/attribute_group', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['attribute_group'] = '';
		$this->data['attribute_group_description'] = array();
		
		$this->template = 'product/attribute_group_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());	
  	}
  	
	private function getUpdateForm() {
     	$this->data['action'] = $this->url->link('product/attribute_group/update', 'token=' . $this->session->data['token'] . '&attribute_group_id=' . $this->request->get['attribute_group_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('product/attribute_group', 'token=' . $this->session->data['token'], 'SSL');

		$this->data['attribute_group'] = $this->model_product_attribute_group->getAttributeGroup($this->request->get['attribute_group_id']);
		$this->data['attribute_group_description'] = $this->model_product_attribute_group->getAttributeGroupDescriptions($this->request->get['attribute_group_id']);
		
		$this->template = 'product/attribute_group_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		$this->response->setOutput($this->render());	
  	}
}
?>